package com.danawa.mds.market.model;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HibernateOrderRepository extends HibernateDaoSupport implements
		OrderRepository {
	
	public void save(Order order) {
		getHibernateTemplate().save(order);
	}

	@SuppressWarnings("unchecked")
	public Order find(String orderId) {
		List<Order> result = (List<Order>) getHibernateTemplate().find(
				"from Order where orderId=?", orderId);
		if (result != null && result.size() > 0) {
			return result.get(0);
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public Set<Order> findByBuyer(Buyer buyer) {
		return new HashSet<Order>(getHibernateTemplate().find(
				"from Order where buyer=?", buyer));
	}

	@SuppressWarnings("unchecked")
	public Set<Order> findAll() {
		return new HashSet<Order>(getHibernateTemplate().loadAll(Order.class));
	}

	public Order delete(Order order) {
		getHibernateTemplate().delete(order);
		return order;
	}

}
